/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石组件
 */
import * as power_property from "../system/power_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
/*
 * 彼岸组件
 */
import * as whale_attack from "./whale_attack";
import * as viper_attack from "./viper_attack";
import * as wasp_attack from "./wasp_attack";
import * as role_attack from "./role_attack";
/**
 * * 领航者发动攻击
 * 
 * @param {server.Entity} entity - 领航者实体对象
 * 
 * @param {string} type - 领航者技能类型
 */
export function RoleLaunchAttack(entity: server.Entity, type: string) {
    // 判断 是否 完成初始化
    if (!entity || !entity.isValid() || !entity.getDynamicProperty('entity:is_initial')) return;
    /**
     * * 获取 角色解锁状态
     */
    const unlock = entity.getDynamicProperty('entity:unlock') as boolean ?? false;
    /**
     * * 获取 战术等级
     */
    const improve = entity.getDynamicProperty('entity:improve') as number ?? 1;
    /**
     * * 获取 实体属性面板
     */
    const getData = power_property.GetProperty(entity);
    /**
     * * 是否暴击
     */
    const erupt = derived_tools.IsErupt(entity);
    /**
     * * 攻击目标
     */
    const target = entity.target;
    /**
     * * 获取 契约玩家 的 唯一标识符
     */
    const user = entity.getDynamicProperty('entity:contract_user');
    // 判断目标是否有效
    if (!target || !target.isValid()) return;
    // 修正实体朝向
    entity.setRotation(math_tools.Vector.difference(entity.location, target.location));
    // 判断 领航者 是否完成 角色契约 且 升级等级大于 4 级
    if (!user || improve <= 4) switch (type) {
        //* 琥珀
        case 'amber': role_attack.Amber(entity, target); break;
        //* 风信
        case 'windnews': role_attack.Moonlight(entity, target, erupt, getData); break;
        //* 默认
        default: role_attack.DefaultAttack(entity, target, erupt, getData); break;
    }
    else switch (type) {
        //* 珍珠水母
        case 'jellyfish_of_pearl': role_attack.jellyfishInPearl(entity, getData); break;
        //* 珍珠游鱼
        case 'fish_of_pearl': role_attack.fishInPearl(entity, target, erupt); break;
        //* 珍珠
        case 'pearl': role_attack.Pearl(entity, target); break;
        //* 琉璃
        case 'crystal': role_attack.Crystal(entity, target, erupt, getData); break;
        //* 蔷薇
        case 'rambler': role_attack.Rambler(entity, getData); break;
        //* 森涅
        case 'sennie': role_attack.SenNie(entity, target, getData); break;
        //* 绯红
        case 'crimson': role_attack.Crimson(entity, target, erupt, getData); break;
        //* 星砂
        case 'starsand': role_attack.StarSand(entity, target, erupt, getData); break;
        //* 月华
        case 'moonlight': role_attack.Moonlight(entity, target, erupt, getData); break;
        //* 海灵
        case 'hailing': role_attack.Hailing(entity, target, erupt, getData); break;
        //* 海娜
        case 'haina': role_attack.Haina(entity, target, erupt, getData); break;
        //* 幽蓝
        case 'dullblue': role_attack.Dullblue(entity, target, erupt, getData); break;
        //* 九九
        case 'ninenine': role_attack.NineNine(entity, target, erupt, getData); break;
        //* 雪隐
        case 'snowhidden': role_attack.SnowHidden(entity, target, erupt, getData); break;
        //* 默认
        default: role_attack.DefaultAttack(entity, target, erupt, getData); break;
    };
    // 判断 升级等级是否大于 5 级
    if (improve >= 5 && !unlock) {
        // 向玩家播放通知
        server.world.getPlayers().forEach(
            player => {
                if (player.id !== user) return;
                /**
                 * * 玩家屏幕标题对象
                 */
                const display = player.onScreenDisplay;
                /**
                 * * 角色位置
                 */
                const roleLocation = math_tools.Vector.copy(entity.getHeadLocation());
                /**
                 * * 玩家位置
                 */
                const playerLocation = math_tools.Vector.copy(player.getHeadLocation());
                /**
                 * * 定义 粒子参数
                 */
                const molang = new server.MolangVariableMap();
                /**
                 ** 粒子射流方向
                 */
                const direction = math_tools.Vector.difference(roleLocation, playerLocation);
                // 设定 玩家屏幕标题
                display.setTitle(native_tools.EntityTranslate(entity));
                // 设置副标题
                display.updateSubtitle({ text: '解锁< 雾海巡游 >机制' });
                // 播放 音效
                player.playSound('block.enchanting_table.use');
                // 设置 粒子参数
                molang.setFloat('variable.type', 0);
                molang.setVector3('variable.direction', direction);
                // 播放射流粒子
                native_tools.TrySpawnParticle(entity.dimension, 'scripts:path_ray', entity.location, molang);
            }
        );
        // 修改实体动态属性
        entity.setDynamicProperty('entity:unlock', true);
    }
};
/**
 * * 野蜂机群 - 攻击
 * 
 * @param {server.Entity} object - 实体对象
 * 
 * @param {string} type - 类型
 */
export function MachineWaspAttack(object: server.Entity, type: string) {
    // 判断 是否 完成初始化
    if (!object || !object.isValid()) return;
    if (!object.getDynamicProperty('entity:is_initial')) return;
    if (!achieve_plan.ControlEventTrigger.EntityToken('攻击冷却', object, 40)) return;
    /**
     * * 是否暴击
     */
    const erupt = derived_tools.IsErupt(object);
    /**
     * * 攻击目标
     */
    const target = object.target;
    // 检测 攻击目标
    if (!target || !target.isValid()) return;
    // 执行细分事件
    switch (type) {
        //* 野蜂 - 侦查者
        case 'detection': wasp_attack.Detection(object, target, erupt); break;
        //* 野蜂 - 君临者
        case 'emperor': wasp_attack.Emperor(object, target); break;
        //* 野蜂 - 维系者
        case 'support': wasp_attack.Support(object); break;
        //* 野蜂 - 维系者
        case 'guide': wasp_attack.Guide(object); break;

        default: break;
    };
};
/**
 * * 渊鲸潜艇 - 攻击
 * 
 * @param {server.Entity} object - 实体对象
 * 
 * @param {string} type - 类型
 */
export function MachineWhaleAttack(object: server.Entity, type: string) {
    // 判断 是否 完成初始化
    if (!object || !object.isValid()) return;
    if (!object.getDynamicProperty('entity:is_initial')) return;
    if (!achieve_plan.ControlEventTrigger.EntityToken('攻击冷却', object, 40)) return;
    /**
     * * 是否暴击
     */
    const erupt = derived_tools.IsErupt(object);
    /**
     * * 攻击目标
     */
    const target = object.target;
    // 检测 攻击目标
    if (!target || !target.isValid()) return;
    // 执行细分事件
    switch (type) {
        //* 渊鲸 - 执行者
        case 'execute': whale_attack.Execute(object, target, erupt); break;

        //* 渊鲸 - 君临者
        case 'emperor': whale_attack.Emperor(object, target, erupt); break;

        default: break;
    };
};
/**
 * * 蝰蛇车队 - 攻击
 * 
 * @param {server.Entity} object - 实体对象
 * 
 * @param {string} type - 类型
 */
export function MachineViperAttack(object: server.Entity, type: string) {
    // 判断 是否 完成初始化
    if (!object || !object.isValid()) return;
    if (!object.getDynamicProperty('entity:is_initial')) return;
    if (!achieve_plan.ControlEventTrigger.EntityToken('攻击冷却', object, 40)) return;
    /**
     * * 攻击目标
     */
    const target = object.target;
    // 检测 攻击目标
    if (!target || !target.isValid()) return;
    // 修正实体朝向
    object.setRotation(math_tools.Vector.difference(object.location, target.location));
    // 执行细分事件
    switch (type) {
        //* 蝰蛇 - 维系者
        case 'support': viper_attack.Support(object, target); break;

        //* 森蚺 - 哨兵炮
        case 'sentinel': viper_attack.PythonSentinel(object, target); break;

        default: break;
    };
};